const { createCategoryError, removeCategoryError, updateCategoryError, getCategoryError, searchCategoryError } = require('../constant/err.type')
const { createCategory, removeCategory, update, getCategoryList, searchCategorylist } = require('../service/category.service')
class CategoryController {

    async addCategory(ctx) {
        // 获取数据
        const { category_name, parent_category_id, status } = ctx.request.body;
        // 操作数据库
        try {
            const res = await createCategory({ category_name, parent_category_id, status })
            //    返回结果
            ctx.body = {
                code: '200',
                message: '添加商品分类成功',
                result: res
            }
        } catch (err) {
            console.log(err)
            ctx.app.emit('error', createCategoryError, ctx)
        }
    }

    async deleteCategory(ctx) {
        try {
            const res = await removeCategory(ctx.params.id)
            if (res) {
                ctx.body = {
                    code: "200",
                    message: "删除商品分类成功",
                    result: ""
                }
            } else {
                return ctx.app.emit("error", removeCategoryError, ctx)
            }

        } catch (err) {
            console.error(err)
        }
    }

    async updateCategory(ctx) {
        const { id, category_name, status = true } = ctx.request.body;
        try {
            const res = await update(id, { category_name, status })
            if (res) {
                ctx.body = {
                    code: "200",
                    message: "修改商品分类成功",
                    result: ""
                }
            } else {
                return ctx.app.emit("error", updateCategoryError, ctx)
            }

        } catch (err) {
            console.error(err)
        }
    }


    async getCategory(ctx) {
        // 解析pageNum,pageSize默认值设置为1，10
        const { pageNum = 1, pageSize = 10 } = ctx.request.query
        try {
            const res = await getCategoryList(pageNum, pageSize)
            if (res) {
                ctx.body = {
                    code: "200",
                    message: "获取商品分类成功",
                    result: res
                }
            } else {
                return ctx.app.emit("error", getCategoryError, ctx)
            }

        } catch (err) {
            console.error(err)
        }
    }

    // 查询商品分类
    async searchCategory(ctx) {
        //    获取post请求的
        const { category_name, pageNum = 1, pageSize = 10 } = ctx.request.body;
        try {
            const res = await searchCategorylist({ category_name, pageNum, pageSize })
            if (res) {
                ctx.body = {
                    code: "200",
                    message: "查询商品分类成功",
                    result: res
                }
            } else {
                return ctx.app.emit("error", searchCategoryError, ctx)
            }
        } catch (err) {
            console.error(err)
        }
    }
}
module.exports = new CategoryController();